/*
 * Copyright 2017-8-30 13:56:26 The JA-SIG Collaborative. All rights reserved.
 * distributed with thi file and available online at
 */
package com.lap.scp.admin.ao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import com.lap.scd.commons.ao.AbstractAO;
import com.lap.scd.commons.constant.RespCode;
import com.lap.scd.commons.page.Page;
import com.lap.scd.commons.page.PageResult;
import com.lap.scd.commons.page.PageUtils;
import com.lap.scd.commons.result.Resp;
import com.lap.scp.admin.ao.ConfigAO;
import com.lap.scp.admin.core.ConfigManager;
import com.lap.scp.admin.domain.ConfigDO;
import com.lap.scp.admin.query.ConfigQueryDO;

/**
 * <pre>
 * 配置应用对象
 * </pre>
 * 
 * @author 系统自动生成 Exp
 * @Date 2017-8-30 13:56:26
 * @since 1.0
 */
@Service("configAO")
public class DefaultConfigAO extends AbstractAO implements ConfigAO {

	@Resource
	private ConfigManager configManager;

	@Override
	public Resp<Integer> deleteById(int configId) {
		Resp<Integer> resp = new Resp<>();
		try {
			int flag = configManager.deleteById(configId);
			resp.setData(flag);
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(configId, e);
		}
		return resp;
	}

	@Override
	public Resp<ConfigDO> insertConfig(ConfigDO configDO, BindingResult error) {
		Resp<ConfigDO> resp = new Resp<>();
		try {
			super.validate(resp, error, configDO);
			if (resp.isSuccess()) {
				configManager.insertConfig(configDO);
				resp.setData(configDO);
			}
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(configDO, e);
		}
		return resp;
	}

	@Override
	public Resp<ConfigDO> queryById(int configId) {
		Resp<ConfigDO> resp = new Resp<>();
		try {
			ConfigDO configDO = configManager.queryById(configId);
			resp.setData(configDO);
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(configId, e);
		}
		return resp;
	}

	@Override
	public PageResult<ConfigDO> queryPage(Page page, ConfigQueryDO configQueryDO) {
		PageResult<ConfigDO> pr = new PageResult<ConfigDO>();
		try {
			List<ConfigDO> list = configManager.queryPage(page, configQueryDO);

			PageUtils.copyPage(pr, page);
			pr.setData(list);
		} catch (Exception e) {
			pr.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(new Object[] { page, configQueryDO }, e);
		}
		return pr;
	}

	@Override
	public Resp<Integer> updateConfig(ConfigDO configDO, BindingResult error) {
		Resp<Integer> resp = new Resp<>();
		try {
			super.validate(resp, error, configDO);
			if (resp.isSuccess()) {
				int flag = configManager.updateConfig(configDO);
				resp.setData(flag);
			}
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(configDO, e);
		}
		return resp;
	}

}
